"use strict";
cc._RF.push(module, '7e3fbzzIElCH78dby8CeOg2', 'slider');
// scripts/engines/components/slider.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../gameEngine");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var SCROLLTYPE;
(function (SCROLLTYPE) {
    SCROLLTYPE[SCROLLTYPE["HORIZONTAL"] = 0] = "HORIZONTAL";
    SCROLLTYPE[SCROLLTYPE["VERTICAL"] = 1] = "VERTICAL";
})(SCROLLTYPE || (SCROLLTYPE = {}));
var slider = /** @class */ (function (_super) {
    __extends(slider, _super);
    function slider() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.scrollType = SCROLLTYPE.HORIZONTAL;
        _this.itemContent = null;
        _this.item = null;
        _this.showItemCount = 3;
        _this.itemInterval = 0;
        _this.selectSpf = null;
        _this.noSelectSpf = null;
        _this.lockSpf = null;
        _this.curMinIndex = 0;
        _this.curMaxIndex = 0;
        _this.minIndex = 0;
        _this.maxIndex = Number.MAX_VALUE;
        _this._curSelectNode = null;
        return _this;
    }
    slider.prototype.start = function () {
        this.addTouchEvent();
    };
    slider.prototype.loadSlider = function (startIndex, startScroll, minIndex, maxIndex) {
        if (startIndex === void 0) { startIndex = 0; }
        if (startScroll === void 0) { startScroll = 0; }
        if (minIndex === void 0) { minIndex = 0; }
        if (maxIndex === void 0) { maxIndex = Number.MAX_VALUE; }
        this.minIndex = minIndex;
        this.maxIndex = maxIndex;
        var createCount = this.showItemCount * 3;
        this.curMinIndex = startIndex;
        this.curMaxIndex = startIndex + createCount;
        for (var i = 0; i < createCount; i++) {
            var node = cc.instantiate(this.item);
            node.active = true;
            this.itemContent.addChild(node);
            var mPos = this.getPosByIndex(i + startIndex);
            var pos = cc.v2();
            switch (this.scrollType) {
                case SCROLLTYPE.HORIZONTAL: {
                    pos = cc.v2(mPos, 0);
                    break;
                }
                case SCROLLTYPE.VERTICAL: {
                    pos = cc.v2(0, mPos);
                    break;
                }
            }
            node.setPosition(pos);
            this.refrenshOneNodeData();
        }
        this.scrollToIndex(startScroll, 0);
    };
    slider.prototype.addTouchEvent = function () {
        this.node.on('touchstart', this.onTouchStart, this);
        this.node.on("touchmove", this.onTouchMove, this);
        this.node.on('touchend', this.onTouchEnd, this);
        this.node.on('touchcancel', this.onTouchCancel, this);
    };
    slider.prototype.removeTouchEvent = function () {
        this.node.off('touchstart', this.onTouchStart, this);
        this.node.off("touchmove", this.onTouchMove, this);
        this.node.off('touchend', this.onTouchEnd, this);
        this.node.off('touchcancel', this.onTouchCancel, this);
    };
    slider.prototype.scrollToIndex = function (index, time) {
        var _this = this;
        var vec2 = cc.v2(0, 0);
        var pos = this.getPosByIndex(index);
        switch (this.scrollType) {
            case SCROLLTYPE.HORIZONTAL: {
                vec2 = cc.v2(-pos, 0);
                break;
            }
            case SCROLLTYPE.VERTICAL: {
                vec2 = cc.v2(0, -pos);
                break;
            }
        }
        //@ts-ignore
        cc.tween(this.itemContent).to(time, { position: vec2 }).call(function () {
            _this.refrenshSpf();
        }).start();
    };
    slider.prototype.getPosByIndex = function (index) {
        var mPos = index * (this.itemInterval + this.item.width);
        switch (this.scrollType) {
            case SCROLLTYPE.HORIZONTAL: {
                mPos = index * (this.itemInterval + this.item.width);
                break;
            }
            case SCROLLTYPE.VERTICAL: {
                mPos = index * (this.itemInterval + this.item.height);
                break;
            }
        }
        return mPos;
    };
    slider.prototype.getIndexByPos = function (pos) {
        switch (this.scrollType) {
            case SCROLLTYPE.HORIZONTAL: {
                return Math.floor(pos.x / (this.itemInterval + this.item.width));
                break;
            }
            case SCROLLTYPE.VERTICAL: {
                return Math.floor(pos.y / (this.itemInterval + this.item.height));
                break;
            }
        }
    };
    slider.prototype.refrenshItemPos = function (detal) {
        var _this = this;
        if (detal < 0) {
            this.itemContent.children.concat().forEach(function (n) {
                switch (_this.scrollType) {
                    case SCROLLTYPE.HORIZONTAL: {
                        var nPos = n.convertToWorldSpaceAR(cc.v2());
                        var cPos = _this.node.convertToWorldSpaceAR(cc.v2());
                        var distance = nPos.sub(cPos).mag();
                        if (distance > gameEngine_1.default.Canvas().width / 2 + _this.item.width * 2 && nPos.x < cPos.x) {
                            var posX = _this.getPosByIndex(_this.curMaxIndex);
                            n.setPosition(cc.v2(posX, n.y));
                            _this.curMaxIndex++;
                            _this.curMinIndex++;
                        }
                        break;
                    }
                    case SCROLLTYPE.VERTICAL: {
                        if (n.y < _this.node.height + _this.item.height * 1.5) {
                        }
                        break;
                    }
                }
            });
        }
        else {
            this.itemContent.children.concat().forEach(function (n) {
                switch (_this.scrollType) {
                    case SCROLLTYPE.HORIZONTAL: {
                        var nPos = n.convertToWorldSpaceAR(cc.v2());
                        var cPos = _this.node.convertToWorldSpaceAR(cc.v2());
                        var distance = nPos.sub(cPos).mag();
                        if (distance > gameEngine_1.default.Canvas().width / 2 + _this.item.width * 2 && nPos.x > cPos.x) {
                            var posX = _this.getPosByIndex(_this.curMinIndex);
                            n.setPosition(cc.v2(posX, n.y));
                            _this.curMaxIndex--;
                            _this.curMinIndex--;
                        }
                        break;
                    }
                    case SCROLLTYPE.VERTICAL: {
                        if (n.y > _this.node.height + _this.item.height * 1.5) {
                        }
                        break;
                    }
                }
            });
        }
    };
    slider.prototype.onTouchStart = function (e) {
        cc.Tween.stopAllByTarget(this.itemContent);
    };
    slider.prototype.onTouchMove = function (e) {
        var detal = e.getDelta();
        var d = 0;
        switch (this.scrollType) {
            case SCROLLTYPE.HORIZONTAL: {
                // 横向滚动
                var xDetal = detal.x;
                var minX = this.getPosByIndex(this.minIndex);
                var maxX = this.getPosByIndex(this.maxIndex);
                if (this.itemContent.x + xDetal > minX) {
                    xDetal = minX - this.itemContent.x;
                }
                else if (this.itemContent.x + xDetal < -maxX) {
                    xDetal = this.itemContent.x + maxX;
                }
                this.itemContent.x += xDetal;
                d = xDetal;
                break;
            }
            case SCROLLTYPE.VERTICAL: {
                // 竖向滚动
                var yDetal = detal.y;
                if (this.itemContent.x + yDetal > 0) {
                    yDetal = 0 - this.itemContent.x;
                }
                this.itemContent.y += yDetal;
                d = yDetal;
                break;
            }
        }
        this.refrenshItemPos(d);
        this.refrenshSpf();
    };
    slider.prototype.onTouchEnd = function (e) {
        cc.Tween.stopAllByTarget(this.itemContent);
        var minDistanceIndex = this.getMinDistanceItemIndex();
        var mNode = this.itemContent.children[minDistanceIndex];
        this._curSelectNode = mNode;
        var cIndex = this.getIndexByPos(this._curSelectNode.getPosition());
        if (cIndex <= gameEngine_1.default.getManagers().peopleArcheryManager.getData().infinityLevel) {
            this._curSelectNode.getComponent(cc.Sprite).spriteFrame = this.selectSpf;
        }
        var pos = this.node.convertToNodeSpaceAR(mNode.convertToWorldSpaceAR(cc.v2()));
        // console.log(minDistanceIndex,"怎么个事",this.curMinIndex,this.getIndexByPos(mNode.getPosition()),mNode);
        // mNode.color = cc.Color.RED;
        //@ts-ignore
        cc.tween(this.itemContent).by(0.2, { position: pos.negate() }).call(function () {
        }).start();
        // this.refrenshSpf();
    };
    slider.prototype.onTouchCancel = function (e) {
        this.onTouchEnd(e);
    };
    slider.prototype.refrenshOneNodeData = function () {
        var _this = this;
        this.itemContent.children.forEach(function (n) {
            var index = _this.getIndexByPos(n.getPosition());
            var label = n.getChildByName("label").getComponent(cc.Label);
            label.string = (index + 1).toString();
        });
    };
    slider.prototype.refrenshScale = function () {
        var _this = this;
        this.itemContent.children.forEach(function (n) {
            var index = _this.getIndexByPos(n.getPosition());
            if (index < _this.minIndex || index > _this.maxIndex) {
                n.scale = 0;
            }
            else {
                var nPos = n.convertToWorldSpaceAR(cc.v2());
                var cPos = _this.node.convertToWorldSpaceAR(cc.v2());
                var distance = nPos.sub(cPos).mag();
                var scale = (gameEngine_1.default.Canvas().width / 2 - distance) / (gameEngine_1.default.Canvas().width / 2) * 0.2 + 0.8;
                n.scale = scale;
            }
            var aNode = n.getChildByName("zhujiemianui__9");
            if (index == _this.maxIndex) {
                aNode.active = false;
            }
            else {
                aNode.active = true;
            }
        });
    };
    slider.prototype.refrenshSpf = function () {
        var _this = this;
        this.itemContent.children.forEach(function (n) {
            var index = _this.getIndexByPos(n.getPosition());
            if (index <= gameEngine_1.default.getManagers().peopleArcheryManager.getData().infinityLevel) {
                n.getComponent(cc.Sprite).spriteFrame = _this.noSelectSpf;
            }
            else {
                n.getComponent(cc.Sprite).spriteFrame = _this.lockSpf;
            }
            // console.log(index,O1HD.getManagers().peopleArcheryManager.getData().infinityLevel);
        });
        var minDistanceIndex = this.getMinDistanceItemIndex();
        var node = this.itemContent.children[minDistanceIndex];
        this._curSelectNode = node;
        var cIndex = this.getIndexByPos(this._curSelectNode.getPosition());
        if (cIndex <= gameEngine_1.default.getManagers().peopleArcheryManager.getData().infinityLevel) {
            this._curSelectNode.getComponent(cc.Sprite).spriteFrame = this.selectSpf;
        }
    };
    slider.prototype.getMinDistanceItemIndex = function () {
        var _this = this;
        var minDistance = 10000000;
        var minDistanceIndex = 0;
        this.itemContent.children.forEach(function (n, index) {
            var nPos = n.convertToWorldSpaceAR(cc.v2());
            var cPos = _this.node.convertToWorldSpaceAR(cc.v2());
            var distance = nPos.sub(cPos).mag();
            if (distance < minDistance) {
                minDistance = distance;
                minDistanceIndex = index;
            }
        });
        return minDistanceIndex;
    };
    slider.prototype.getCurSelectIndex = function () {
        return this.getIndexByPos(this._curSelectNode.getPosition());
    };
    slider.prototype.update = function (dt) {
        this.refrenshOneNodeData();
        this.refrenshScale();
    };
    __decorate([
        property({ type: cc.Enum(SCROLLTYPE), tooltip: "滚动方向" })
    ], slider.prototype, "scrollType", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "itemContent节点" })
    ], slider.prototype, "itemContent", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "item节点" })
    ], slider.prototype, "item", void 0);
    __decorate([
        property({ tooltip: "显示的节点数量" })
    ], slider.prototype, "showItemCount", void 0);
    __decorate([
        property({ tooltip: "每个节点间隔距离" })
    ], slider.prototype, "itemInterval", void 0);
    __decorate([
        property({ type: cc.SpriteFrame, tooltip: "被选中的节点的图片" })
    ], slider.prototype, "selectSpf", void 0);
    __decorate([
        property({ type: cc.SpriteFrame, tooltip: "未选中的节点的图片" })
    ], slider.prototype, "noSelectSpf", void 0);
    __decorate([
        property({ type: cc.SpriteFrame, tooltip: "未解锁图片" })
    ], slider.prototype, "lockSpf", void 0);
    slider = __decorate([
        ccclass
    ], slider);
    return slider;
}(cc.Component));
exports.default = slider;

cc._RF.pop();